package com.jie.server.mvc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jie.server.mvc.mapper.GroupMapper;
import com.jie.server.mvc.mapper.GroupRelevanceMapper;
import com.jie.server.mvc.mapper.UserMapper;
import com.jie.server.mvc.model.entity.Group;
import com.jie.server.mvc.model.entity.GroupRelevance;
import com.jie.server.mvc.model.entity.User;
import com.jie.server.mvc.model.vo.GroupQueryVo;
import com.jie.server.mvc.model.vo.GroupUpdateVo;
import com.jie.server.mvc.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 群表 服务实现类
 * </p>
 *
 * @author XieZhiJie
 * @since 2022-04-24
 */
@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService {

    private final GroupMapper groupMapper;

    private final GroupRelevanceMapper groupRelevanceMapper;

    private final UserMapper userMapper;

    @Autowired
    public GroupServiceImpl(
            GroupMapper groupMapper,
            GroupRelevanceMapper groupRelevanceMapper,
            UserMapper userMapper
    ) {
        this.groupMapper = groupMapper;
        this.groupRelevanceMapper = groupRelevanceMapper;
        this.userMapper = userMapper;
    }

    @Override
    public boolean build(int userId, Group group) {
        group.setGroupId(null);
        if ("".equals(group.getGroupName().trim())) {
            return false;
        }
        group.setCreateTime(LocalDateTime.now());
        this.groupMapper.insert(group);
        GroupRelevance groupRelevance = new GroupRelevance();
        groupRelevance.setGroupId(group.getGroupId());
        groupRelevance.setUserId(userId);
        this.groupRelevanceMapper.insert(groupRelevance);
        return true;
    }

    @Override
    public Page<Group> getList(GroupQueryVo groupQueryVo) {
        Page<Group> groupPage = new Page<>(groupQueryVo.getCurrent(), groupQueryVo.getLimit());
        return this.groupMapper.selectPage(groupPage, null);
    }

    @Override
    public Page<Group> getUserList(int userId, GroupQueryVo groupQueryVo) {
        QueryWrapper<GroupRelevance> groupRelevanceQueryWrapper = new QueryWrapper<>();
        groupRelevanceQueryWrapper.eq("user_id", userId);
        groupRelevanceQueryWrapper.select("group_id");
        // 查询出当前用户的拥有的群数据
        List<GroupRelevance> groupRelevanceList = this.groupRelevanceMapper.selectList(groupRelevanceQueryWrapper);
        // 封装群id
        List<Integer> idLists = new ArrayList<>(groupRelevanceList.size());
        for (GroupRelevance groupRelevance : groupRelevanceList) {
            idLists.add(groupRelevance.getGroupId());
        }
        QueryWrapper<Group> groupQueryWrapper = new QueryWrapper<>();
        if (idLists.size() == 0) {
            // 设置一个无法获取的值
            groupQueryWrapper.eq("group_id", -1);
        } else {
            groupQueryWrapper.in("group_id", idLists);
        }
        // 封装分页信息
        Page<Group> groupPage = new Page<>(groupQueryVo.getCurrent(), groupQueryVo.getLimit());
        return this.groupMapper.selectPage(groupPage, groupQueryWrapper);
    }

    @Override
    public boolean updateGroupName(Integer userId, GroupUpdateVo groupUpdateVo) {
        // 1.判空处理
        if (groupUpdateVo.getGroupId() == null || groupUpdateVo.getGroupName() == null || "".equals(groupUpdateVo.getGroupName().trim())) {
            return false;
        }
        // 2.查询当前用户是否在这个群聊里
        QueryWrapper<GroupRelevance> groupRelevanceQueryWrapper = new QueryWrapper<>();
        groupRelevanceQueryWrapper.eq("user_id", userId);
        groupRelevanceQueryWrapper.eq("group_id", groupUpdateVo.getGroupId());
        if (this.groupRelevanceMapper.selectOne(groupRelevanceQueryWrapper) == null) {
            return false;
        }
        Group group = new Group();
        group.setGroupId(groupUpdateVo.getGroupId());
        group.setGroupName(groupUpdateVo.getGroupName());
        return this.groupMapper.updateById(group) > 0;
    }

    @Override
    public boolean joinGroup(Integer userId, Integer groupId) {
        // 1.判断该群是否存在
        if (this.groupMapper.selectById(groupId) == null) {
            return false;
        }
        // 2.判断该用户是否已加入群
        QueryWrapper<GroupRelevance> groupRelevanceQueryWrapper = new QueryWrapper<>();
        groupRelevanceQueryWrapper.eq("user_id", userId);
        groupRelevanceQueryWrapper.eq("group_id", groupId);
        if (this.groupRelevanceMapper.selectOne(groupRelevanceQueryWrapper) != null) {
            return false;
        }
        // 3.加入该用户
        GroupRelevance groupRelevance = new GroupRelevance();
        groupRelevance.setGroupId(groupId);
        groupRelevance.setUserId(userId);
        return this.groupRelevanceMapper.insert(groupRelevance) > 0;
    }

    @Override
    public boolean signOutGroup(Integer userId, Integer groupId) {
        // 1.判断该群是否存在
        if (this.groupMapper.selectById(groupId) == null) {
            return false;
        }
        // 2.判断该用户是否已加入群
        QueryWrapper<GroupRelevance> groupRelevanceQueryWrapper = new QueryWrapper<>();
        groupRelevanceQueryWrapper.eq("user_id", userId);
        groupRelevanceQueryWrapper.eq("group_id", groupId);
        if (this.groupRelevanceMapper.selectOne(groupRelevanceQueryWrapper) == null) {
            return false;
        }
        // 3.删除该群信息
        return this.groupRelevanceMapper.delete(groupRelevanceQueryWrapper) > 0;
    }

    @Override
    public List<User> getGroupUserList(Integer groupId) {
        // 不需要判断该用户是否在群里
        QueryWrapper<GroupRelevance> groupRelevanceQueryWrapper = new QueryWrapper<>();
        groupRelevanceQueryWrapper.eq("group_id", groupId);
        List<GroupRelevance> groupRelevanceList = this.groupRelevanceMapper.selectList(groupRelevanceQueryWrapper);
        if (groupRelevanceList.size() == 0) {
            return new ArrayList<>();
        }
        List<Integer> userIdList = new ArrayList<>(groupRelevanceList.size());
        for (GroupRelevance groupRelevance : groupRelevanceList) {
            userIdList.add(groupRelevance.getUserId());
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.in("user_id", userIdList);
        userQueryWrapper.select("user_id", "user_account", "username");
        return this.userMapper.selectList(userQueryWrapper);
    }

}
